package com.killer.catalina;

import com.killer.servlet.HttpServlet;
import com.killer.servlet.ServletRequest;
import com.killer.servlet.ServletResponse;
import com.killer.view.Request;
import com.killer.view.Response;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;

/**
 * 匹配不到相关Servlet时，默认处理的Servlet，主要处理静态资源
 */
public class DefaultServlet extends HttpServlet{

    private String appDir;

    public DefaultServlet(String appDir) {
        this.appDir = appDir;
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws IOException {
        Request request_=(Request)request;
        Response response_ = (Response) response;
        //返回静态资源  /webDemo01/index.html
        //资源请求路径
        String path = request_.getPath();
        String resourcePath = path.substring(path.indexOf("/", 1));
        File resourceFile = new File(appDir+File.separator+resourcePath);
        response_.outputStatisticResource(resourceFile);
    }
}
